# Vulkan 性能分析系统 - 一键测试脚本
# 用于快速编译、安装、运行和分析性能数据

# 颜色定义
$Green = "Green"
$Red = "Red"
$Yellow = "Yellow"
$Cyan = "Cyan"

function Write-ColorText {
    param(
        [string]$Text,
        [string]$Color = "White"
    )
    Write-Host $Text -ForegroundColor $Color
}

function Write-Step {
    param([string]$Message)
    Write-Host ""
    Write-ColorText "========================================" $Cyan
    Write-ColorText "  $Message" $Cyan
    Write-ColorText "========================================" $Cyan
}

function Write-Success {
    param([string]$Message)
    Write-ColorText "✓ $Message" $Green
}

function Write-Error {
    param([string]$Message)
    Write-ColorText "✗ $Message" $Red
}

function Write-Info {
    param([string]$Message)
    Write-ColorText "ℹ $Message" $Yellow
}

# 检查当前目录
if (-not (Test-Path "entry") -or -not (Test-Path "hvigorfile.ts")) {
    Write-Error "请在项目根目录运行此脚本"
    Write-Info "当前目录应包含: entry/, hvigorfile.ts 等"
    exit 1
}

# 主菜单
Write-Step "Vulkan 性能分析系统 - 一键测试"
Write-Host ""
Write-Host "请选择操作:"
Write-Host "  1. 完整测试流程 (编译 + 安装 + 运行 + 分析)"
Write-Host "  2. 仅编译项目"
Write-Host "  3. 仅安装应用"
Write-Host "  4. 仅运行应用 (2分钟)"
Write-Host "  5. 仅导出性能数据"
Write-Host "  6. 仅分析性能数据"
Write-Host "  7. 验证集成状态"
Write-Host "  0. 退出"
Write-Host ""
$choice = Read-Host "请输入选项 (0-7)"

switch ($choice) {
    "1" {
        Write-Step "步骤 1/6: 验证集成"
        python verify_integration.py
        if ($LASTEXITCODE -ne 0) {
            Write-Error "集成验证失败,请先修复问题"
            exit 1
        }
        
        Write-Step "步骤 2/6: 编译项目"
        .\hvigorw.bat assembleHap
        if ($LASTEXITCODE -ne 0) {
            Write-Error "编译失败"
            exit 1
        }
        Write-Success "编译成功"
        
        Write-Step "步骤 3/6: 安装应用"
        $hapPath = "entry\build\default\outputs\default\entry-default-signed.hap"
        if (-not (Test-Path $hapPath)) {
            Write-Error "HAP 文件不存在: $hapPath"
            exit 1
        }
        hdc install $hapPath
        if ($LASTEXITCODE -ne 0) {
            Write-Error "安装失败"
            exit 1
        }
        Write-Success "安装成功"
        
        Write-Step "步骤 4/6: 运行应用 (2分钟测试)"
        Write-Info "请手动启动应用或使用以下命令:"
        Write-Host "  hdc shell aa start -a EntryAbility -b <your_bundle_name>"
        Write-Host ""
        Write-Info "应用将运行 120 秒..."
        Write-Host "按任意键开始计时..." -NoNewline
        $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        Write-Host ""
        
        # 倒计时 120 秒
        for ($i = 120; $i -gt 0; $i--) {
            Write-Progress -Activity "应用运行中" -Status "剩余时间: $i 秒" -PercentComplete ((120 - $i) / 120 * 100)
            Start-Sleep -Seconds 1
        }
        Write-Progress -Activity "应用运行中" -Completed
        Write-Success "测试时间到"
        
        Write-Step "步骤 5/6: 停止应用并导出数据"
        Write-Info "请输入应用的 Bundle Name (例如: com.example.testapp):"
        $bundleName = Read-Host "Bundle Name"
        
        if ($bundleName) {
            Write-Info "正在停止应用..."
            hdc shell aa force-stop $bundleName
            Start-Sleep -Seconds 2
            
            Write-Info "正在导出性能数据..."
            $remotePath = "/data/app/el2/100/base/$bundleName/haps/entry/files"
            hdc file recv "$remotePath/profiling_summary.csv" ".\oh_profiling_summary.csv"
            hdc file recv "$remotePath/profiling_detailed.csv" ".\oh_profiling_detailed.csv"
            
            if (Test-Path "oh_profiling_summary.csv") {
                Write-Success "数据导出成功"
            } else {
                Write-Error "数据导出失败,请检查文件路径"
                Write-Info "尝试查找文件位置:"
                hdc shell find /data/app -name "profiling_*.csv" 2>$null
                exit 1
            }
        } else {
            Write-Error "未输入 Bundle Name,跳过数据导出"
        }
        
        Write-Step "步骤 6/6: 分析性能数据"
        if (Test-Path "oh_profiling_summary.csv") {
            python analyze_performance.py oh_profiling_summary.csv
            Write-Success "分析完成"
        } else {
            Write-Error "找不到性能数据文件"
        }
        
        Write-Step "完整测试流程完成"
        Write-Success "所有步骤已完成!"
        Write-Info "性能数据已保存到:"
        Write-Host "  - oh_profiling_summary.csv (汇总数据)"
        Write-Host "  - oh_profiling_detailed.csv (详细数据)"
    }
    
    "2" {
        Write-Step "编译项目"
        .\hvigorw.bat assembleHap
        if ($LASTEXITCODE -eq 0) {
            Write-Success "编译成功"
            Write-Info "HAP 文件位置: entry\build\default\outputs\default\entry-default-signed.hap"
        } else {
            Write-Error "编译失败"
        }
    }
    
    "3" {
        Write-Step "安装应用"
        $hapPath = "entry\build\default\outputs\default\entry-default-signed.hap"
        if (-not (Test-Path $hapPath)) {
            Write-Error "HAP 文件不存在: $hapPath"
            Write-Info "请先编译项目 (选项 2)"
            exit 1
        }
        hdc install $hapPath
        if ($LASTEXITCODE -eq 0) {
            Write-Success "安装成功"
        } else {
            Write-Error "安装失败"
        }
    }
    
    "4" {
        Write-Step "运行应用"
        Write-Info "请输入应用的 Bundle Name (例如: com.example.testapp):"
        $bundleName = Read-Host "Bundle Name"
        Write-Info "请输入 Ability Name (默认: EntryAbility):"
        $abilityName = Read-Host "Ability Name"
        if (-not $abilityName) {
            $abilityName = "EntryAbility"
        }
        
        Write-Info "启动应用..."
        hdc shell aa start -a $abilityName -b $bundleName
        
        if ($LASTEXITCODE -eq 0) {
            Write-Success "应用已启动"
            Write-Info "应用将运行 120 秒..."
            
            # 倒计时
            for ($i = 120; $i -gt 0; $i--) {
                Write-Progress -Activity "应用运行中" -Status "剩余时间: $i 秒" -PercentComplete ((120 - $i) / 120 * 100)
                Start-Sleep -Seconds 1
            }
            Write-Progress -Activity "应用运行中" -Completed
            Write-Success "测试时间到"
            
            Write-Info "停止应用..."
            hdc shell aa force-stop $bundleName
            Write-Success "应用已停止"
        } else {
            Write-Error "启动失败"
        }
    }
    
    "5" {
        Write-Step "导出性能数据"
        Write-Info "请输入应用的 Bundle Name (例如: com.example.testapp):"
        $bundleName = Read-Host "Bundle Name"
        
        if ($bundleName) {
            Write-Info "正在导出性能数据..."
            $remotePath = "/data/app/el2/100/base/$bundleName/haps/entry/files"
            
            Write-Info "导出汇总数据..."
            hdc file recv "$remotePath/profiling_summary.csv" ".\oh_profiling_summary.csv"
            
            Write-Info "导出详细数据..."
            hdc file recv "$remotePath/profiling_detailed.csv" ".\oh_profiling_detailed.csv"
            
            if (Test-Path "oh_profiling_summary.csv") {
                Write-Success "数据导出成功"
                Write-Info "文件已保存到:"
                Write-Host "  - oh_profiling_summary.csv"
                Write-Host "  - oh_profiling_detailed.csv"
            } else {
                Write-Error "数据导出失败"
                Write-Info "尝试查找文件位置:"
                hdc shell find /data/app -name "profiling_*.csv" 2>$null
            }
        } else {
            Write-Error "未输入 Bundle Name"
        }
    }
    
    "6" {
        Write-Step "分析性能数据"
        if (Test-Path "oh_profiling_summary.csv") {
            Write-Info "正在分析 OpenHarmony 性能数据..."
            python analyze_performance.py oh_profiling_summary.csv
            Write-Success "分析完成"
        } elseif (Test-Path "android_profiling_summary.csv") {
            Write-Info "正在分析 Android 性能数据..."
            python analyze_performance.py android_profiling_summary.csv
            Write-Success "分析完成"
        } else {
            Write-Error "找不到性能数据文件"
            Write-Info "请先导出数据 (选项 5)"
        }
        
        # 检查是否有双平台数据
        if ((Test-Path "oh_profiling_summary.csv") -and (Test-Path "android_profiling_summary.csv")) {
            Write-Host ""
            Write-Info "检测到双平台数据,是否进行对比分析? (Y/N)"
            $compare = Read-Host
            if ($compare -eq "Y" -or $compare -eq "y") {
                Write-Info "正在进行对比分析..."
                python analyze_performance.py oh_profiling_summary.csv android_profiling_summary.csv
                Write-Success "对比分析完成"
            }
        }
    }
    
    "7" {
        Write-Step "验证集成状态"
        python verify_integration.py
        if ($LASTEXITCODE -eq 0) {
            Write-Success "集成验证通过"
        } else {
            Write-Error "集成验证失败"
        }
    }
    
    "0" {
        Write-Info "退出"
        exit 0
    }
    
    default {
        Write-Error "无效选项"
        exit 1
    }
}

Write-Host ""
Write-ColorText "========================================" $Cyan
Write-ColorText "脚本执行完毕" $Green
Write-ColorText "========================================" $Cyan
Write-Host ""
Write-Info "更多帮助请参考:"
Write-Host "  - QUICKSTART.md (快速开始指南)"
Write-Host "  - INTEGRATION_COMPLETE.md (集成完成报告)"
Write-Host "  - PERFORMANCE_PROFILING_README.md (完整文档)"
Write-Host ""
